﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;

namespace Touch
{
    public partial class _Category : System.Web.UI.Page
    {
        Boolean bIsFree;
        LocalFunctions FUNC;
        protected Category cat;
        protected int iCatID;
        protected int iTotalResults;
        protected PageType _pageType;
        protected string _SearchTerm;
        private bool isTop;
        private bool haveSubCats = false;
        private int iTotalPages;
        private int iStartNum;
        public String sIphoneStyleHideBanner = String.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            FUNC = new LocalFunctions();
            
            GetParameters();
            int TotalResults = 0;
            List<Application> Items = null;

            switch (_pageType)
            {
                case PageType.Regular:
                    cat = FUNC.GetCategoryByID(iCatID);
                    //liPaid.Attributes.Add("class", "active");
                    switch (cat.id)
                    {
                        case 25:
                            haveSubCats = true;
                            Items = FUNC.GetAppsByCatID(iCatID, iStartNum, 5, out iTotalResults);
                            TotalResults = 5;
                            break;
                        case 39:
                            haveSubCats = true;
                            Items = FUNC.GetAppsByCatID(iCatID, iStartNum, 5, out iTotalResults);
                            TotalResults = 5;
                            break;
                        case 30:
                            haveSubCats = true;
                            Items = FUNC.GetAppsByCatID(iCatID, iStartNum, 5, out iTotalResults);
                            TotalResults = 5;
                            break;
                        case 19:
                            haveSubCats = true;
                            Items = FUNC.GetAppsByCatID(iCatID, iStartNum, 5, out iTotalResults);
                            TotalResults = 5;
                            break;
                        case 27:
                            haveSubCats = true;
                            Items = FUNC.GetAppsByCatID(iCatID, iStartNum, 5,  out iTotalResults);
                            TotalResults = 5;
                            break;
                        default:
                            Items = FUNC.GetAppsByCatID(iCatID, iStartNum, 30, out iTotalResults);
                            break;
                    }
                    break;

                case PageType.Reccomended:
                    cat = new Category();
                    cat.name = "אפליקציות מומלצות";
                    Items = FUNC.GetRecomendedApps(iStartNum, 30, out iTotalResults);
                    break;

                case PageType.Newest:
                    cat = new Category();
                    cat.name = "אפליקציות חדשות";
                    Items = FUNC.GetNewApps(iStartNum, 30, out iTotalResults);
                    break;

                case PageType.Popular:
                    cat = new Category();
                    cat.name = "אפליקציות פופולריות";
                    Items = FUNC.GetPopularApps(iStartNum, 30, out iTotalResults);
                    break;

                case PageType.Search:
                    cat = new Category();
                    cat.name = "תוצאות חיפוש";
                    Items = FUNC.SearchApps(_SearchTerm, iStartNum, 30, out iTotalResults);

                    if (Items.Count == 0)
                    {
                        //NaveSearchResults = false;
                        Items = FUNC.GetPopularApps(iStartNum, 30, out iTotalResults);
                    }
                    break;

                case PageType.Free:
                    //liFree.Attributes.Add("class", "active");
                    cat = FUNC.GetCategoryByID(iCatID);
                    Items = FUNC.GetFreeApps(iCatID, iStartNum, 30, out iTotalResults);
                    break;
            }

            string sText=string.Empty;
            if (cat.parentid > 0)
            {
                Category parentCat = FUNC.GetCategoryByID(cat.parentid);
                sText = string.Format("<li><a href='Category.aspx?id={0}'>{1}</a></li>", parentCat.id, parentCat.name);
            }
            sText+=string.Format("<li><a href='#'>{0}</a></li>",cat.name);
            ((AppStore)Master).sBreadcrumbs = sText;

            
            //rptApps.DataSource = Items;
            //rptApps.DataBind();
            ReadFirstData();

            if (haveSubCats)
            {
                divTabs.Visible = false;
                //tabs.Visible = false;

                List<Category> lstCategories = FUNC.GetSubCategoriesByID(cat.id);

                rptCategories.DataSource = lstCategories;
                rptCategories.DataBind();
            }
            else
            {
                secCats.Visible = false;
            }

        }

        private void ReadFirstData()
        {
            string sRequest = string.Format("MoreApps.aspx?id={0}&str={1}&type={2}&pagesize=5", iCatID, _SearchTerm, (int)_pageType);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://orangemobile.tapuz.co.il/apps/test/touch/" + sRequest);
            // Set credentials to use for this request.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(receiveStream);

            string st = readStream.ReadToEnd();
            if (st.Trim() != "totalItems:0")
            {
                string sTotalResults = st.Substring(0, st.IndexOf("<li"));
                sTotalResults = "5";
                TotalResults.Value = sTotalResults;

                st = st.Substring(st.IndexOf("<li"));
                ulContent.InnerHtml += st;
                response.Close();
                readStream.Close();
            }
            else
            {
                st = "<li class=\"clearfix\">" +
                                "<p class=\"text info\">" +
                                    "לא נמצאו תוצאות." +
                    "</p><div class=\"cut\">" +
                        "<span class=\"shadow\"></span><span class=\"highlight\"></span>" +
                    "</div><!-- cut --></li>";
                ulContent.InnerHtml += st;
            }
        }

        private void GetParameters()
        {
            iCatID = Convert.ToInt32(Request.QueryString["id"]);
            isTop = Convert.ToBoolean(Convert.ToInt32(Request.QueryString["Top"]));
            _pageType = (PageType)Convert.ToInt32(Request.QueryString["type"]);
            _SearchTerm = (Request.QueryString["src"] != null) ? Request.QueryString["src"].ToString() : string.Empty;
        }
    }
}